package com.c1802.util;

import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;

import com.alibaba.druid.pool.DruidDataSource;



public class AliPool {
	static DruidDataSource ds;
	
	//静态代码块     只运行一次
	static{
		String fname="druid.properties";
		InputStream file=AliPool.class.getClassLoader().getResourceAsStream(fname);
		Properties prop=new Properties();
		try {
			prop.load(file);
		} catch (IOException e1) {
			e1.printStackTrace();
		}
		
		String driverClassName=prop.getProperty("driverClassName");
		String jdbcUrl=prop.getProperty("jdbcUrl");
		String username=prop.getProperty("username");
		String password=prop.getProperty("password");
		String initialSize=prop.getProperty("initialSize");
		String minIdle=prop.getProperty("minIdle");
		String maxActive=prop.getProperty("maxActive");
		String maxWait=prop.getProperty("maxWait");
		//读取文件
//		System.out.println(driverClassName);
//		System.out.println(jdbcUrl);
//		System.out.println(username);
//		System.out.println(password);
//		System.out.println(initialSize);
//		System.out.println(minIdle);
//		System.out.println(maxActive);
//		System.out.println(maxWait);
		
		//配置连接池
		ds=new DruidDataSource();
		ds.setDriverClassName(driverClassName);
		ds.setUrl(jdbcUrl);
		ds.setUsername(username);
		ds.setPassword(password);
		ds.setInitialSize(new Integer(initialSize));
		ds.setMinIdle(new Integer(minIdle));
		ds.setMaxActive(new Integer(maxActive));
		ds.setMaxWait(new Integer(maxWait));
	}
	
	//池里链接
	public static Connection getConn(){

		Connection conn=null;
		try {
			conn = ds.getConnection();
			System.out.println(conn);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return conn;
	}

}
